import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login'
import home from '@/views/home'
import layout from '@/views/layout' 
import Article from '@/views/article'

Vue.use(VueRouter)

const routes = [
 {
   path:'/login',
   component:login
 },
 {
  path:'/',
  component:layout,
  children:[
    {
      path:'',
      component:home
    },
    {
      path:'/article',
      component:Article
    }
  ]
 }
]



const router = new VueRouter({
  routes
})

//拦截
router.beforeEach((to,from,next)=>{
  const user = JSON.parse(window.localStorage.getItem('user'))
  if(to.path!=='/login'){
    if(user){
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }
})
export default router
